#coding:utf-8
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
import urllib.response
import urllib.request
import urllib.parse
import http.cookiejar
import re
import os

 

def get_html(url):
	# headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
	# urllib.request.
	# page = urllib.request.urlopen(url, headers=headers) #打开网页
	# htmlcode = page.read() #读取页面源码6
#	return repr(htmlcode)
	headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15'}
	request = urllib.request.Request(url, headers=headers)
	data = urllib.request.urlopen(request).read()
	return repr(data)

def write_string_to_text(str, path):
	pageFile = open(path, 'w')
	pageFile.write(str)
	pageFile.close()

def download_image(imglist):
	print(imglist)
	make_folder("imageSource")
	x = 0
	for img in imglist:
		print(img + "\n")
		try:
			urllib.request.urlretrieve(img, 'imageSource/%s.jpg' %x)
		except Exception as e:
			print(e)
		x += 1
    
def make_folder(path):
    if os.path.exists(path) is False:
    	os.makedirs(path)


#url_str = 'https://cn.bing.com/images/search?q=flower&FORM=HDRSC2'
url_str = 'https://588ku.com/so/tu/?h=bd&sem=1&kw=bd0093838&bd_vid=8527252228538822707'
#url_str = "https%3a%2f%2fcn.bing.com%2fimages%2fsearch%3fq%3d%e9%b2%9c%e8%8a%b1%26FORM%3dHDRSC2"


# url = urllib.parse.quote(url_str)
# print(url)

html_string = get_html(url_str)

write_string_to_text(html_string, "pageCode.text")

#reg = r'src="(.+?\.jpg)" pic_ext' #正则表达式

#reg = r'https://\S+?.jpg|https://\S+?.png'
prefix='data-original='
#reg = r'src=\S+"'

reg = prefix + r'\S+"'


reg_img = re.compile(reg)#编译一下，运行更快
imglist = reg_img.findall(html_string)


print(imglist)

processingUrls = []
for originUrl in imglist:
	if len(originUrl) > len(prefix):
		processingUrl = originUrl[len(prefix):].strip(r'"')
		reg = r'//\S+?.jpg|//\S+?.png'
		reg_img = re.compile(reg)#编译一下，运行更快
		if len(reg_img.findall(processingUrl)) == 1:
			processingUrls.append("https:" + processingUrl)

print(processingUrls)
download_image(processingUrls)

